import { createRouter, createWebHistory } from 'vue-router'
import { ElLoading, ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'login',
      component: () => import('@/views/login/LoginView.vue'),
      meta: {
        title: '登录',
      },
    },
    {
      path: '/layout',
      name: 'layout',
      component: () => import('@/views/layout/LayoutView.vue'),
      children: [
        {
          path: '/system',
          name: 'system',
          redirect: '/system/role',
          meta: {
            title: '系统管理',
          },
          children: [
            {
              path: '/system/role',
              name: 'role',
              component: () => import('@/views/system/role/RoleView.vue'),
              meta: {
                title: '角色管理',
              },
            },
            {
              path: '/system/operator',
              name: 'operator',
              component: () => import('@/views/system/operator/OperatorView.vue'),
              meta: {
                title: '操作员管理',
              },
            },
            {
              path: '/system/bank',
              name: 'bank',
              component: () => import('@/views/system/bank/BankView.vue'),
              meta: {
                title: '银行管理',
              },
            },
            {
              path: '/system/log',
              name: 'log',
              component: () => import('@/views/system/log/LogView.vue'),
              meta: { title: '操作日志', icon: 'Menu' },
            },
            {
              path: '/system/message-notification',
              name: 'message-notification',
              component: () =>
                import('@/views/system/message-notification/MessageNotificationView.vue'),
              meta: { title: '消息通知', icon: 'Menu' },
            },
          ],
        },
        {
          path: '/goods-type',
          name: 'goods-type',
          component: () => import('@/views/goods-type/GoodsTypeView.vue'),
          meta: { title: '商品类别', icon: 'Menu' },
        },
        {
          path: '/home',
          name: 'home',
          component: () => import('@/views/home/HomeView.vue'),
          meta: {
            title: '首页',
          },
        },
        {
          path: '/goods-info',
          name: 'goods-info',
          component: () => import('@/views/goods-info/GoodsInfoView.vue'),
          meta: {
            title: '商品管理',
          },
        },
        {
          path: '/goods-receipt',
          name: 'goods-receipt',
          component: () => import('@/views/goods-receipt/GoodsReceiptView.vue'),
          meta: {
            title: '商品入库单据管理',
          },
        },
        {
          path: '/good-store',
          name: 'good-store',
          component: () => import('@/views/good-store/GoodStoreView.vue'),
          meta: { title: '商品库存', icon: 'Menu' },
        },
        {
          path: '/goods-price',
          name: 'goods-price',
          component: () => import('@/views/goods-price/GoodsPriceView.vue'),
          meta: { title: '调价单管理', icon: 'Menu' },
        },
        {
          path: '/goods-sales',
          name: 'goods-sales',
          component: () => import('@/views/goods-sales/GoodsSalesView.vue'),
          meta: { title: '商品上架管理', icon: 'Menu' },
        },
        {
          path: '/goods-outbound',
          name: 'goods-outbound',
          component: () => import('@/views/goods-outbound/GoodsOutboundView.vue'),
          meta: { title: '商品出库管理', icon: 'Menu' },
        },
        {
          path: '/good-oder-from',
          name: 'good-oder-from',
          component: () => import('@/views/good-oder-from/GoodOderFormView.vue'),
          meta: { title: '商品订单管理', icon: 'Menu' },
        },
        {
          path: '/good-send',
          name: 'good-send',
          component: () => import('@/views/good-send/GoodSendView.vue'),
          meta: { title: '已发货订单', icon: 'Menu' },
        },
        {
          path: '/test',
          name: 'test',
          component: () => import('@/views/TestView.vue'),
          meta: {
            title: '测试',
          },
        },
        {
          path: '/good-settle',
          name: 'good-settle',
          component: () => import('@/views/good-settle/GoodSettleView.vue'),
          meta: { title: '订单结算台', icon: 'Menu' },
        },
        {
          path: '/good-settle-history',
          name: 'good-settle-history',
          component: () => import('@/views/good-settle-history/GoodSettleHistoryView.vue'),
          meta: { title: '我的结算单', icon: 'Menu' },
        },
        {
          path: '/goods-removal',
          name: 'goods-removal',
          component: () => import('@/views/goods-removal/GoodsRemovalView.vue'),
          meta: { title: '商品下架管理', icon: 'Menu' },
        },
        {
          path: '/goods-online',
          name: 'goods-online',
          component: () => import('@/views/goods-online/GoodsOnlineView.vue'),
          meta: { title: '商品上架管理', icon: 'Menu' },
        },
        {
          path: '/operation-history',
          name: 'operation-history',
          component: () => import('@/views/operation-history/OperationHistoryView.vue'),
          meta: { title: '操作历史', icon: 'Menu' },
        },
        {
          path: '/good-comments',
          name: 'good-comments',
          component: () => import('@/views/good-comments/GoodCommentsView.vue'),
          meta: { title: '商品评论管理', icon: 'Menu' },
        },
        {
          path: '/good-return',
          name: 'good-return',
          component: () => import('@/views/good-return/GoodReturnView.vue'),
          meta: { title: '退货管理', icon: 'Menu' },
        },
        {
          path: '/good-delivery-fee',
          name: 'good-delivery-fee',
          component: () => import('@/views/good-delivery-fee/GoodDeliveryFeeView.vue'),
          meta: { title: '商品配送费', icon: 'Menu' },
        },
      ],
    },
  ],
})
let loadingInstance = null
// 全局前置守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  // 显示加载动画
  loadingInstance = ElLoading.service({
    lock: true,
    text: '页面加载中...',
  })

  // 检查用户是否已登录
  const token = userStore.token
  // 如果访问登录页，且已登录，则重定向到首页
  if (to.name === 'login' && token) {
    next({ name: 'home' })
    return
  }

  // 如果不是登录页，但未登录，则重定向到登录页
  if (to.name !== 'login' && !token) {
    ElMessage.warning('您尚未登录或登录已过期，请重新登录')
    next({ name: 'login' })
    return
  }
  next()
})

router.afterEach(() => {
  loadingInstance.close()
})

export default router
